﻿using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for MenuInfo
/// </summary>
public class MenuInfo
{
    private int id;

    public int Id
    {
        get { return id; }
        set { id = value; }
    }

    private int parentId;

    public int ParentId
    {
        get { return parentId; }
        set { parentId = value; }
    }

    private string displayName;

    public string DisplayName
    {
        get { return displayName; }
        set { displayName = value; }
    }

    private string navigateUrl;

    public string NavigateUrl
    {
        get { return navigateUrl; }
        set { navigateUrl = value; }
    }

    private int sortIndex;

    public int SortIndex
    {
        get { return sortIndex; }
        set { sortIndex = value; }
    }

    private string icon;

    public string Icon
    {
        get { return icon; }
        set { icon = value; }
    }

    private bool disabled;

    public bool Disabled
    {
        get { return disabled; }
        set { disabled = value; }
    }

    public void ChangeParent()
    {
        const string sql = "update lt_Menu set m_parentid=@parentid where m_id=@id";
        SqlHelper.Instance.ExecuteNonQuery(sql, CreateParameters());
    }

    public void ChangeSortIndex()
    {
        const string sql = "update lt_Menu set m_sortIndex=@sortIndex where m_id=@id";
        SqlHelper.Instance.ExecuteNonQuery(sql, CreateParameters());
    }

    public void ChangeDisable()
    {
        const string sql = "update lt_Menu set m_disabled=@disabled where m_id=@id";
        SqlHelper.Instance.ExecuteNonQuery(sql,
            SqlParameterEx.CreateParameter("@disabled", SqlDbType.Bit, disabled),
            SqlParameterEx.CreateParameter("@id", SqlDbType.Int, id));
    }

    public void Add()
    {
        const string sql = "INSERT INTO [lt_Menu]([m_parentid], [m_name], [m_url], [m_sortIndex], [m_icon])VALUES(@parentid, @name, @url, @sortIndex, @icon)";
        sortIndex = GetMaxSortIndex();
        SqlHelper.Instance.ExecuteNonQuery(sql, CreateParameters());
    }

    public void Update()
    {
        const string sql = "UPDATE [lt_Menu] SET [m_parentid]=@parentid, [m_name]=@name, [m_url]=@url, [m_sortIndex]=@sortIndex, [m_icon]=@icon WHERE [m_id]=@id";
        SqlHelper.Instance.ExecuteNonQuery(sql, CreateParameters());
    }

    public void Delete()
    {
        const string sql = "DELETE [lt_Menu] WHERE [m_id]=@id or [m_parentid]=@id";
        SqlHelper.Instance.ExecuteNonQuery(sql, CreateParameters());
    }

    private int GetMaxSortIndex()
    {
        const string sql = "SELECT MAX(m_sortIndex) FROM [lt_Menu]";
        object max = SqlHelper.Instance.ExecuteScalar(sql);
        if (max == null)
            return 1;
        return int.Parse(max.ToString());
    }

    private SqlParameter[] CreateParameters()
    {
        SqlParameter[] parameteres = new SqlParameter[]{
            SqlParameterEx.CreateParameter("@id", SqlDbType.Int, id),        
            SqlParameterEx.CreateParameter("@parentid", SqlDbType.Int, parentId),
            SqlParameterEx.CreateParameter("@name", SqlDbType.VarChar, 50, displayName),
            SqlParameterEx.CreateParameter("@url", SqlDbType.VarChar, 200, navigateUrl),
            SqlParameterEx.CreateParameter("@sortIndex", SqlDbType.TinyInt, sortIndex),
            SqlParameterEx.CreateParameter("@icon", SqlDbType.VarChar, 200, icon)
        };
        return parameteres;
    }
}